//
//  ViewController.m
//  DCDownloadOpration
//
//  Created by Wade on 16/3/30.
//  Copyright © 2016年 Wade. All rights reserved.
//

#import "ViewController.h"
#import "DownloadTableViewCell.h"

#import <MediaPlayer/MediaPlayer.h>
#import "DCDownloadOprationManager.h"
#import "DCOSSDDownloadOpration.h"
#import "CPDownloadInfo.h"
#import "CPCourseZipSecurityInfo.h"
#import "CPDownloadTools.h"


@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSArray *dataList;
@property (nonatomic, strong) DCDownloadOprationManager *manager;

@property (nonatomic, strong) DCOSSDDownloadOpration *OSSDDownloadOpration;

@property (nonatomic, strong) CPDownloadTools *downloadTools;

@end

@implementation ViewController

- (CPDownloadTools *)downloadTools
{
    if (!_downloadTools) {
        _downloadTools = [CPDownloadTools shareDownloadTools];
        _downloadTools.maxQueueNum = 2;
    }
    return _downloadTools;
}

/**
 *  https://dn-capps.qbox.me/1.mp4
 */

- (DCDownloadOprationManager *)manager
{
    if(!_manager)
    {
        _manager = [DCDownloadOprationManager managerWithIdentifier:@"downloadVideo"];
        _manager.maxDownloadingCount = 2;
    }
    return _manager;
}

- (NSArray *)dataList
{
    if (!_dataList) {
        NSMutableArray *temp = [NSMutableArray array];
        for (int i = 1; i <= 10; i++) {
            NSString *urlStr = [NSString stringWithFormat:@"https://dn-capps.qbox.me/%d.mp4",i];
            [temp addObject:urlStr];
        }
        
        _dataList = temp;
    }
    
    return _dataList;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    /*
     {
     coursename = CA2 EP Writing Unit 1-5 V1_5;
     md5_key = 5c4b90f5197c4e89915f60b904ad5b03;
     old_course_version = ;
     onlineinfo = {
     api_secret = CFB7F14AB1031092DEE29438AE5FDB57;
     messageroomid = 665A94A7113A4048BB5E838ED7AC330C;
     meettingid = 1325813029;
     zcode = 3705290605;
     api_key = D76FBB1C42BEF3D1431C494818F1E29F;
     }
     ;
     class_item_id = 665A94A7113A4048BB5E838ED7AC330C;
     classtype = 1;
     lessonmode = Coaching;
     oid = course/5879DBB9751B4E8D897991D67112A5E8;
     start_time = 2016-08-09 10:00:00;
     class_item_name = catOnlineAndOffline--CA2 EP Writing Unit 1-5 V1_5--WRITING 2A;
     lesson_id = 35DFA746116243718BE4561731B85CA6;
     old_update_descriptions = ;
     username = dc_teacher;
     lessonname = {"en":"WRITING 2A","zh":""};
     class_id = BF1A165FE1DF4055A92789F9D79B367E;
     class_room_id = 89C219541B85459C8F0D2D9913C4E5CA;
     ie = 0;
     lesson_url = http://course.classserver.cn/course/5879DBB9751B4E8D897991D67112A5E8;
     p1 = 0;
     old_lesson_id = ;
     end_time = 2016-08-09 11:00:00;
     current_course_version = 1.00;
     current_update_descriptions = 1.5;
     p2 = 0;
     course_id = 47F18E339B6040EFBD3CA4F903C546D9;
     }

     */
    
  
    
    NSString *unzipPath1 = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"1.zip"];
    NSLog(@"Path-%@",unzipPath1);
    NSDictionary *dict = @{@"destinationPath":unzipPath1,@"downloadType":@2,@"downloadURL":@"0",@"osskey":@"course/0FAB797780A84473B0770D368D18D2A5",@"unzipPath":[unzipPath1 stringByDeletingPathExtension],@"totalSize":@0,@"courseSecurityInfo":@{@"header":@"",@"key":@"",@"md5":@""}};
//    CPDownloadInfo *downloadInfo = [self.downloadTools downloadInfoWithCourseJson:dict];

    [self.downloadTools cp_downloadCoursePakageZipWithDonloadJson:dict progress:^(NSInteger currentLength, NSInteger totalLength, float pro) {
        NSLog(@"下载进度1--%f",pro);
    } donwloadComplition:^(NSError *error) {
        NSLog(@"下载完成1--%@",error);
    } unzipProgress:^(float pro) {
        NSLog(@"解压进度1--%f",pro);
    } decryptMediaProgress:^(float pro) {
        NSLog(@"解密进度1--%f",pro);
    }];
    
    
    NSString *unzipPath2 = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"2.zip"];
    NSDictionary *dict1 = @{@"destinationPath":unzipPath2,@"downloadType":@2,@"downloadURL":@"1",@"osskey":@"course/414B0FA56E454AEE8BDB2AA272C7D508",@"unzipPath":[unzipPath2 stringByDeletingPathExtension],@"totalSize":@0,@"courseSecurityInfo":@{@"header":@"",@"key":@"",@"md5":@""}};
    
    [self.downloadTools cp_downloadCoursePakageZipWithDonloadJson:dict1 progress:^(NSInteger currentLength, NSInteger totalLength, float pro) {
        NSLog(@"下载进度2--%f",pro);
    } donwloadComplition:^(NSError *error) {
        NSLog(@"下载完成2--%@",error);
    } unzipProgress:^(float pro) {
        NSLog(@"解压进度2--%f",pro);
    } decryptMediaProgress:^(float pro) {
        NSLog(@"解密进度2--%f",pro);
    }];
    
    NSString *unzipPath3 = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"3.zip"];
    
    NSDictionary *dict2 = @{@"destinationPath":unzipPath3,@"downloadType":@2,@"downloadURL":@"2",@"osskey":@"course/EAFE3A74C918451486D9AB5CD1C24F11",@"unzipPath":[unzipPath3 stringByDeletingPathExtension],@"totalSize":@0,@"courseSecurityInfo":@{@"header":@"",@"key":@"",@"md5":@""}};
    [self.downloadTools cp_downloadCoursePakageZipWithDonloadJson:dict2 progress:^(NSInteger currentLength, NSInteger totalLength, float pro) {
        NSLog(@"下载进度3--%f",pro);
    } donwloadComplition:^(NSError *error) {
        NSLog(@"下载完成3--%@",error);
    } unzipProgress:^(float pro) {
        NSLog(@"解压进度3--%f",pro);
    } decryptMediaProgress:^(float pro) {
        NSLog(@"解密进度3--%f",pro);
    }];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DownloadTableViewCell *cell = [DownloadTableViewCell cellWithTableView:tableView];
    cell.urlStr = self.dataList[indexPath.row];
    cell.manager = self.manager;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   

}

@end
